<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model.trim="params.className"
                        placeholder="班级名称"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAdd">新建班级</Button>
            </div>
        </div>
        <Table :data="list" :columns="tableColumns" border align="center">
            <template slot-scope="{ row }" slot="classNo">
                <div>{{ row.classNo }}</div>
            </template>
            <template slot-scope="{ row }" slot="className">
                <div>{{ row.className }}</div>
            </template>
            <template slot-scope="{ row }" slot="teacherName">
                <div>{{ row.teacherName }}</div>
            </template>
            <template slot-scope="{ row }" slot="enterYear">
                <div>{{ row.enterYear }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" @click="handleEdit(row.classId)">编辑</Button>
                    <Button type="text" @click="handleStudent(row.classId)">学生管理</Button>
                    <Button type="text" @click="handleDelete(row.classId)">删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { mapState } from 'vuex'
import { queryClassList, delClass } from '@/api/classManage'
export default {
    inject: ['nextPageReload'],
    data () {
        return {
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10,
                className: ''
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '班级编号', slot: 'classNo', align: 'center' },
                { title: '班级名称', slot: 'className', align: 'center' },
                { title: '任课教师', slot: 'teacherName', align: 'center' },
                { title: '所属专业', key: 'majorName', align: 'center' },
                { title: '入学学年', slot: 'enterYear', align: 'center' },
                { title: '班级人数', key: 'stuNum', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 150 }
            ])
        }
    },
    computed: {
        ...mapState({
            isTeacher: state => state.user.roleType === '3'
        }),
        tableColumns () {
            if (this.isTeacher) {
                return this.columns.filter(item => item.slot !== 'teacherName')
            }
            return this.columns
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            queryClassList(this.params).then(res => {
                if (res.data.code === '0000') {
                    this.list = res.data.data.list
                    this.params.total = res.data.data.total
                }
            })
        },
        clearSearch () {
            this.params.className = ''
            this.params.pageNum = 1
            this.queryList()
        },
        handleAdd () {
            this.nextPageReload()
            this.$router.push({
                name: 'classManageAdd'
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'classManageEdit',
                params: {
                    id
                }
            })
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '删除班级将同时删除本班级下所有学生，确认是否删除？',
                onOk: (id) => {
                    delClass(id).then(res => {
                        if (res.data.data != '0000') {
                            this.$Message.error(res.data.msg)
                            return
                        }
                        this.$Message.success('删除成功')
                    })
                }
            })
        },
        handleStudent (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'stuManageList',
                params: {
                    classId: id
                }
            })
        }
    }
}
</script>
